// 引用 node-rsa 加密模块
var NodeRSA = require('node-rsa');

// npm install node-rsa
function rsaEncrypt() {
    pubKey = new NodeRSA(publicKey,'pkcs8-public');
    var encryptedData = pubKey.encrypt(text, 'base64');
    return encryptedData
}

function rsaDecrypt() {
    priKey = new NodeRSA(privatekey,'pkcs8-private');
    var decryptedData = priKey.decrypt(encryptedData, 'utf8');
    return decryptedData
}

var key = new NodeRSA({b: 512});                    //生成512位秘钥
var publicKey = key.exportKey('pkcs8-public');    //导出公钥
var privatekey = key.exportKey('pkcs8-private');  //导出私钥


var text = "15335353535"

var encryptedData = rsaEncrypt()
var decryptedData = rsaDecrypt()

// console.log("公钥:\n", publicKey)
// console.log("私钥:\n", privatekey)
console.log(btoa(publicKey));
console.log(btoa(privatekey));
console.log("publicKey: ", publicKey)
console.log("privatekey: ", privatekey)
console.log("加密字符串: ", encryptedData, typeof encryptedData)
console.log("解密字符串: ", decryptedData)


console.log(Buffer.from("NHVOQ1c0YWtLY3pEUGJvRA==", 'base64').toString())